package top.happylaishop.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.happylaishop.shop.dao.CategoryDao;
import top.happylaishop.shop.dto.CategoryDto;
import top.happylaishop.shop.dto.UserDto;
import top.happylaishop.shop.pojo.Category;
import top.happylaishop.shop.service.CategoryService;
import top.happylaishop.shop.utils.resp.BaseResponse;
import top.happylaishop.shop.utils.resp.RestResponse;

import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public BaseResponse add(CategoryDto categoryDto, UserDto userDto) {
        Category category = BeanUtil.copyProperties(categoryDto, Category.class);
        category.setId(null);
        category.setCreateTime(new Date());
        category.setUpdateTime(category.getCreateTime());
        category.setCreateUserId(userDto.getId());
        category.setUpdateUserId(userDto.getId());
        categoryDao.insert(category);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse update(CategoryDto categoryDto, UserDto userDto) {
        Category category = BeanUtil.copyProperties(categoryDto, Category.class);
        category.setUpdateTime(new Date());
        category.setUpdateUserId(userDto.getId());
        categoryDao.updateById(category);
        return BaseResponse.success();
    }

    @Override
    public RestResponse<List<Category>> listChildrenParallelCategory(Integer categoryId) {
        LambdaQueryWrapper<Category> lambdaQueryWwrapper = new LambdaQueryWrapper<>();
        lambdaQueryWwrapper.eq(Category::getParentId, categoryId);
        return RestResponse.defaultSucessData(categoryDao.selectList(lambdaQueryWwrapper));
    }

    @Override
    public RestResponse<List<Integer>> listCategoryAndChildrenById(Integer categoryId) {
        return null;
    }
}
